package com.kexin.common.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kexin.common.exception.WebException;
import com.kexin.common.numeration.ExceptionEnum;
import com.kexin.common.user.entity.UserBase;
import com.kexin.common.user.service.CouponFaceService;
import com.kexin.common.utils.HttpClient;
import com.kexin.common.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chen
 * @date 2021/7/23
 */
@Service
public class CouponFaceServiceImpl implements CouponFaceService {

    @Value("${auth.baseUrl}")
    private  String baseUrl;

    @Value("${auth.appId}")
    private  String appId;

    Logger log = LoggerFactory.getLogger(CouponFaceServiceImpl.class);

    @Autowired
    HttpClient httpClient;
    @Override
    public Result checkToken(String token) {
        String url = baseUrl+"/auth-center/client/checkToken";
        Map<String,Object> map = new HashMap<>();
        map.put("appId",appId);
        map.put("token",token);
        ResponseEntity<String> rsp = httpClient.post(url,map);
        String body = rsp.getBody();
        JSONObject json = JSONObject.parseObject(body);
        if (rsp.getStatusCode() == HttpStatus.OK){
            if (!"0".equals(json.getString("code"))){
                log.error("请求用户中心获校验token信息异常："+body);
            }
        }else {
            log.error("请求用户中心获校验token信息网络异常");
            throw new WebException(ExceptionEnum.ERROR_INTERNAL_SERVER.getCode(),"系统繁忙，请稍后再试！");
        }
        Result result =  new Result();
        Integer code = json.getInteger("code");
        String msg = json.getString("msg");
        JSONObject data = json.getJSONObject("data");
        result.setCode(code);
        result.setMsg(msg);
        result.setSuccess(false);
        result.setData(data);
        return result;
    }

    @Override
    public UserBase getInfo(String token) {
    	System.out.println("token="+token);
        String url = baseUrl+"/digital-life-basic/user/getInfo";
        Map<String,Object> map = new HashMap<>();
        map.put("appId",appId);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization","Bearer "+token);
        ResponseEntity<String> rsp= httpClient.postJson(url,headers,map);
        String body = rsp.getBody();
        if (rsp.getStatusCode() == HttpStatus.OK){
            JSONObject json = JSONObject.parseObject(body);
            if ("0".equals(json.getString("code"))){
                UserBase userBase = json.getJSONObject("data").toJavaObject(UserBase.class);
                return  userBase;
            }
        }else {
            log.error("请求用户中心获取用户信息网络异常");
            throw new WebException(ExceptionEnum.ERROR_INTERNAL_SERVER.getCode(),"系统繁忙，请稍后再试！");
        }
        log.error("请求用户中心获取用户信息异常："+body);
        throw new WebException(ExceptionEnum.ERROR_NOLOGIN);
    }
}
